package band.wukong.practice.springboot.spring4.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author wukong(wukonggg@139.com)
 */
@Aspect
@Component
class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("@annotation(band.wukong.practice.springboot.spring4.aop.Action)")
    void annotationPointcut() {
    }

    @Before("execution(* band.wukong.practice.springboot.spring4.aop.DemoMethodService.*(..))")
    void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        logger.info("方法规则式拦截: " + method.getName());
    }

    @After("annotationPointcut()")
    void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        logger.info("注解式拦截: " + action.name());
    }



}
